package com.platform.modules.sys.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.annotation.RedisCache;
import com.platform.common.utils.JedisUtil;
import com.platform.common.utils.Query;
import com.platform.modules.sys.dao.SysDictDao;
import com.platform.modules.sys.entity.SysDictEntity;
import com.platform.modules.sys.service.SysDictService;

/**
 * 数据字典Service实现类
 *  SysDictService
 * @author 肖亮
 */
@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {
	@Autowired
	JedisUtil jedisUtil;

	@Override
//	@RedisCache
	public List<SysDictEntity> queryAll(Map<String, Object> params) {
		return baseMapper.queryAll(params);
	}
	
	@Override
	public List<SysDictEntity> queryList(Map<String, Object> params) {
		
		return baseMapper.queryAll(params);
	}

	@Override
	public IPage queryPage(Map<String, Object> params) {
		IPage<SysDictEntity> page = new Query<SysDictEntity>(params).getPage();
		return page.setRecords(baseMapper.selectDictPage(page, params));
	}

	@Override
	public void add(SysDictEntity sysDict) {
		this.save(sysDict);
		try {
			jedisUtil.delByClass(this.getClass().getName(), "queryAll");
		} catch (Exception e) {
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SysDictEntity sysDict) {
		this.updateById(sysDict);
		try {
			jedisUtil.delByClass(this.getClass().getName(), "queryAll");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	public void delete(String id) {
		this.removeById(id);
		jedisUtil.delByClass(this.getClass().getName(), "queryAll");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String[] ids) {
		this.removeByIds(Arrays.asList(ids));
		jedisUtil.delByClass(this.getClass().getName(), "queryAll");
	}

	@Override
	public List<SysDictEntity> queryByCode(Map<String, Object> params) {
		return baseMapper.queryByCode(params);
	}
	@Override
	public SysDictEntity queryByValueAndGroupId(Map<String, Object> params){
		return baseMapper.queryByValueAndGroupId(params);
	}
	
	@Override
	public String getNameByValueAndGroupId(String value,String groupId){

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("value", value);
		params.put("groupId", groupId);
		if(StringUtils.isNotBlank(value) && StringUtils.isNotBlank(groupId)){
			SysDictEntity sysDictEntity = baseMapper.queryByValueAndGroupId(params);
			if(sysDictEntity != null){
				return sysDictEntity.getName();
			}else{
				return "";
			}
		}else{
			return "";
		}
	}
	
	@Override
	public List<SysDictEntity> queryByCodeAndParentId(Map<String, Object> params) {
		return baseMapper.queryByCodeAndParentId(params);
	}
}
